package api

import (
	"alertmanagerWebhook/common"
	"alertmanagerWebhook/config"
	"alertmanagerWebhook/global"
	"alertmanagerWebhook/utils"
	"fmt"
)

// SendEmail sends an email using SMT
// SendToEmail processes the incoming notification and sends appropriate emails.
func SendToEmail(notification config.Notification) error {
	global.Logger.Info("进入邮箱告警通知渠道")
	// Transform the notification into email messages
	emailMessage, err := common.SendEmailNotification(notification)
	if err != nil {
		return fmt.Errorf("failed to transform notification to email: %w", err)
	}

	// Send the email using the generated message
	err = utils.SendEmail(emailMessage.Subject, emailMessage.Body)
	if err != nil {
		return fmt.Errorf("failed to send email: %w", err)
	}

	global.Logger.Infof("Email sent successfully with subject: %s", emailMessage.Subject)
	return nil
}
